//
//  SurpriseCollectionCell.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/12.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SDWebImage

class SurpriseCollectionCell: BaseCollectionCell {
    
    @IBOutlet weak var topImageView: UIImageView!
    
    @IBOutlet weak var nameTextNabel: UILabel!
    
    @IBOutlet weak var indetorView: UIActivityIndicatorView!

    @IBOutlet weak var progressBottView: UIView!
    
    @IBOutlet weak var progessViewW: NSLayoutConstraint!
    @IBOutlet weak var progressLabel: UILabel!
    
    @IBOutlet weak var progressTopView: UIView!
    @IBOutlet weak var botStrMargin: NSLayoutConstraint!
    
    
    var model : SupriseDataListModel?{
    
    
        didSet{
            if model == nil {
                return
            }
            
            
            indetorView.startAnimating()
            topImageView.sd_setImage(with: URL(string :model!.goods_info!.goods_image ), placeholderImage: UIImage.defaulImage(), options: []) { (image, _, _, _) in
                self.indetorView.stopAnimating()
                //                self.imageView1.contentMode = .scaleToFill
                
            }

        
          nameTextNabel.text = model?.goods_info?.goods_name
            
            progessViewW.constant = 0
            
            let temp = model!.od_progress.replacingOccurrences(of: "%", with: "")
            
            
            if let prosess = Float(temp) {
                
                
                progessViewW.constant = ((KScreenW - 5) * 0.5 - 10) / 100 * CGFloat(prosess)
                
                
            }
            
            
            
            progressLabel.attributedText = attributedString(str: "抢宝进度\(model!.od_progress)", font: UIFont.systemFont(ofSize: 12))
        
        
        }
    
    
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        contentView.backgroundColor = UIColor.white
        botStrMargin.constant = 0
        progressTopView.addCornerAndBorder(borderColor: nil, borderWidth: 0, masksToBounds: true, cornerRadius: 3)
        progressBottView.addCornerAndBorder(borderColor: nil, borderWidth: 0, masksToBounds: true, cornerRadius: 3)
        progressLabel.textColor = UIColor.dakGray()
        
             // Initialization code
    }

}


extension SurpriseCollectionCell{


    func attributedString(str: String, font: UIFont) -> NSAttributedString? {
        
        
        let patten = "[\u{4e00}-\u{9fa5}]{1,}"
        //创建正则表达式对象
        
        //caseInsensitive  忽略大小写  最常用
        //dotMatchesLineSeparators 允许 . 匹配任意字符，包括回车换行
        let regex = try! NSRegularExpression(pattern: patten, options: NSRegularExpression.Options.caseInsensitive)
        
        //根据规则匹配指定字符串中第一个符合规则的字符串，并且返回一个NSTextCheckingResult
        let res1 = regex.matches(in: str, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range: NSRange(location: 0, length: str.characters.count))
        
        // 根据原有字符串生成属性字符串
        let strM = NSMutableAttributedString(string: str)
        
        strM.addAttribute(NSFontAttributeName, value: font, range:NSMakeRange(0, str.characters.count))
        
        strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.themeColor(), range:NSMakeRange(0, str.characters.count) )
        
        for temp in res1{
            
            
            strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.dakGray(), range:temp.range )
            
        }
        
            
        
        return strM
        
        
    }
    
    



}
